package com.demo.house.controller;


import com.demo.house.common.Result;
import com.demo.house.entity.Repair;
import com.demo.house.service.RepairService;
import com.demo.house.vo.request.RequestRepairVo;
import com.demo.house.vo.response.ResponseRepairSelectLandlordVo;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@RestController
@RequestMapping("/repair")
public class RepairController {
    @Autowired
    private RepairService repairService;

    /**
     * 新增报修
     * @param repair
     * @return
     */
    @PostMapping ("save")
    public Result save(@RequestBody Repair repair){
        return Result.getSuccess().setData(repairService.insertSelective(repair));
    }
    /*上传图片*/
    @PostMapping(value = "/upp")
    public Result upp(MultipartFile file, @RequestParam("id") Integer id,@RequestParam Integer houseSourseId,@RequestParam Integer userId,@RequestParam Integer urlId){
        return Result.getSuccess().setData(repairService.upp(id,houseSourseId,userId,urlId,file));
    }

    /**
     * 租客查询报修
     * @param id
     * @return
     */
    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        return Result.getSuccess().setData(repairService.selectById(id));

    }

    /**
     * 租客删除报修
     * @param id
     * @return
     */
    @GetMapping("deleteByid/{id}")
    public Result deleteByid(@PathVariable Integer id){

        return Result.getSuccess().setData(repairService.deleteByPrimaryKey(id));
    }


    /**
     * 房东查询保修
     * @param id
     * @return
     */
    @GetMapping("selectRepair/{id}")
    public Result selectRepair(@PathVariable Integer id){
        return Result.getSuccess().setData(repairService.selectRepair(id));
    }
    /**审核报修
     * @param id
     * @return
     */
    @GetMapping("Review/{id}")
    public Result Review(@PathVariable Integer id){
        return Result.getSuccess().setData(repairService.Review(id));
    }
    /*显示图片*/
    @SneakyThrows
    @GetMapping("getRepairUrl")
    public void getRepairUrl(Integer id,Integer urlId, HttpServletResponse response){

        System.out.println(id);
        System.out.println("urlid...."+urlId);
        BufferedImage img = repairService.getRepairUrl(id,urlId);
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(img,"jpg",outputStream);
        IOUtils.closeQuietly(outputStream);
        try {
            outputStream.flush();
        }finally {
            outputStream.close();
        }
    }
}
